lost data on array after "loop"

Hi Scripting guy,

I used one of the tutorials I found at this website on reading excel file sheet. Its working fine on my need only problem is I get an error on the  objFile.Write arrSheet part once I start writing  from array number 8 above.  Please help thanks in advance.

Option Explicit
Dim arrSheet, intCount, outFile, objFSO, objFile, MyEP, MyRS

't500 set assuming 16 sites max on channel Map explore t500 change to ubound value
arrSheet = ReadExcel( MyEP, MyRS, "B6", "L500", False ) 'set to true if you do not need headers

'setup to assign output of readexcel as loadfile name
x = split(loadfile,".")
filename = x(0)
WScript.Echo "check van = " & filename

'to write results to a file
outFile = "C:\Temp\simulation_function\"& file &".txt" 'feb 6

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(outFile,True) 
For intCount = 0 To UBound( arrSheet, 2 )
'to write to cmd use below wscript.Echo
'WScript.Echo arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount )


objFile.Write arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount ) & vbTab & arrSheet( 2, intCount ) & vbTab & arrSheet(3, intCount ) & vbTab & arrSheet( 4, 

intCount ) & vbTab & arrSheet( 5, intCount ) & vbTab & arrSheet( 6, intCount ) & vbTab &  arrSheet( 7, intCount ) & vbTab &  arrSheet( 8, intCount )  & vbCrLf


Next

objFile.Close
WScript.Echo "==============="


'=================================================
'===========Read excel function ==================
'=================================================
Function ReadExcel( myXlsFile, mySheet, my1stCell, myLastCell, blnHeader )
' Function :  ReadExcel
' Version  :  3.00
' This function reads data from an Excel sheet without using MS-Office
'
' Arguments:
' myXlsFile   [string]   The path and file name of the Test Program to be read by the script
' mySheet     [string]   The name of the Channnel Map of the worksheet that needs to be read (could be many in an excel file)
' my1stCell   [string]   The index of the first cell to be read from channel map
' myLastCell  [string]   The index of the last cell to be read from channel map
' blnHeader   [boolean]  True if the first row in the sheet is a header
'
' Returns:
' The values read from the Excel sheet are returned in a two-dimensional
' array; the first dimension holds the columns, the second dimension holds
' the rows read from the Excel sheet.

Dim   arrData( ), i, j
Dim objExcel, objRS
Dim strHeader, strRange

Const adOpenForwardOnly = 0
Const adOpenKeyset      = 1
Const adOpenDynamic     = 2
Const adOpenStatic      = 3

' Define header parameter string for Excel object
If blnHeader Then
strHeader = "HDR=YES;"
Else
strHeader = "HDR=NO;"
End If



' Open the object for the Excel file
Set objExcel = CreateObject( "ADODB.Connection" )
' With IMEX=1 numbers won't be ignored; tip by Thomas Willig.
' Connection string updated by Marcel Ninkemper to open Excel 2007 (.xslx) files.
objExcel.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" _
           & myXlsFile & ";Extended Properties=""Excel 12.0;IMEX=1;" _
           & strHeader & """"

' Open a recordset object for the sheet and range
Set objRS = CreateObject( "ADODB.Recordset" )
strRange = mySheet & "$" & my1stCell & ":" & myLastCell
objRS.Open "Select * from [" & strRange & "]", objExcel, adOpenStatic

' Read the data from the Excel sheet
i = 0
Do Until objRS.EOF

' Stop reading when an empty row is encountered in the Excel sheet
If IsNull( objRS.Fields(0).Value ) Or Trim( objRS.Fields(0).Value ) = "" Then Exit Do


' Add a new row to the output array
ReDim Preserve arrData( objRS.Fields.Count - 1, i )  


' Copy the Excel sheet's row values to the array "row"
' IsNull test credits: Adriaan Westra
For j = 0 To objRS.Fields.Count - 1
If IsNull( objRS.Fields(j).Value ) Then
arrData( j, i ) = " test "
Else
arrData( j, i ) = Trim( objRS.Fields(j).Value )

End If
Next

' Move to the next row
objRS.MoveNext
' Increment the array "row" number
i = i + 1
Loop

' Return the results


' Close the file and release the objects
objRS.Close
objExcel.Close
Set objRS    = Nothing
Set objExcel = Nothing


ReadExcel = arrData



End Function


  • Edited by Mckulet 23 hours 1 minutes ago edited code
February 8th, 2015 7:32am

Can you narrow this down a little so that respondents do not have to wade through all 127 lines of script? It would also help if you gave the code some structure, e.g. by rewriting this block

For j = 0 To objRS.Fields.Count - 1
If IsNull( objRS.Fields(j).Value ) Then
arrData( j, i ) = " test "
Else
arrData( j, i ) = Trim( objRS.Fields(j).Value )
End If
Next

like so:
For j = 0 To objRS.Fields.Count - 1
    If IsNull( objRS.Fields(j).Value ) Then
        arrData( j, i ) = " test "
    Else
        arrData( j, i ) = Trim( objRS.Fields(j).Value )
    End If
Next

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 8:47am

Can you narrow this down a little so that respondents do not have to wade through all 127 lines of script? It would also help if you gave the code some structure, e.g. by rewriting this block

For j = 0 To objRS.Fields.Count - 1
If IsNull( objRS.Fields(j).Value ) Then
arrData( j, i ) = " test "
Else
arrData( j, i ) = Trim( objRS.Fields(j).Value )
End If
Next

like so:
For j = 0 To objRS.Fields.Count - 1
    If IsNull( objRS.Fields(j).Value ) Then
        arrData( j, i ) = " test "
    Else
        arrData( j, i ) = Trim( objRS.Fields(j).Value )
    End If
Next

sorry for that.. the quoted part on your reply is reaeding the specified row and column of a sheet...  on below part the  arrSheet( 8, intCount )  will give me an error that the subscript is out of range no error if i want to return the values from 0 - 7...

objFile.Write arrSheet( 0, intCount ) & vbTab & arrSheet( 1, intCount ) & vbTab & arrSheet( 2, intCount ) & vbTab & arrSheet(3, intCount ) & vbTab & arrSheet( 4, 

intCount ) & vbTab & arrSheet( 5, intCount ) & vbTab & arrSheet( 6, intCount ) & vbTab &  arrSheet( 7, intCount ) & vbTab &  arrSheet( 8, intCount )  & vbCrLf

 
February 8th, 2015 8:53am

Your count is greater than the length of the array.  Perhaps you need to debug your code.

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 9:40am

Your count is greater than the length of the array.  Perhaps you need to debug your code.

February 8th, 2015 9:47am

This line is wrong:

arrSheet = ReadExcel( MyEP, MyRS, "B6", "L500", False ) 'set to true if you do not need headers

Requires the name of a workbook and a worksheet name.

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 10:03am

This line is wrong:

arrSheet = ReadExcel( MyEP, MyRS, "B6", "L500", False ) 'set to true if you do not need headers

Requires the name of a workbook and a worksheet name.

February 8th, 2015 10:14am

This is an example of the correct way to read a spreadsheet with OLEDB. Try it and you will see how it works.  Just supply workbook, worksheet and field names.  The output will be tab delimited.

't500 set assuming 16 sites max on channel Map explore t500 change to ubound value
ReadExcel "c:\scripts\book1.xlsx", "Sheet1$", "Date,Workstation", True 

Function ReadExcel( myXlsFile, mySheet, strFields, bHeader )

	Set objExcel = CreateObject( "ADODB.Connection" )
	objExcel.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & myXlsFile & ";Extended Properties=Excel 12.0"
	Set objRS = objExcel.Execute( "Select " & strFields & " from [" & mySheet & "]")

	If bHeader Then
		For Each f in objRS.Fields
			WScript.StdOut.Write Trim(f.Name) & vbTab
		Next
		WScript.StdOut.WriteLine ""		
	End If
	
	While Not objRS.EOF
		For Each f in objRS.Fields
			WScript.StdOut.Write Trim(f.Value) & vbTab
		Next
		WScript.StdOut.WriteLine ""
		objRS.MoveNext
	Wend
	
 End Function

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 10:26am

Note that you can modify the "SELECT" statement to sort and filter the data in any way needed.

Example:
    "SELECT [Employee Name],[Employee Dept], EmpID,[Hire Date] FROM [mysheet$] WHERE [Employee Dept]  LIKE 'NYC%' ORDER BY [Employee Name]"

Queries only return the used range.

February 8th, 2015 10:36am

Note that you can modify the "SELECT" statement to sort and filter the data in any way needed.

Example:
    "SELECT [Employee Name],[Employee Dept], EmpID,[Hire Date] FROM [mysheet$] WHERE [Employee Dept]  LIKE 'NYC%' ORDER BY [Employee Name]"

Queries only return the used range.

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 7:42pm

Note that the sheet name has to end with a dollar sign.  If sheet is named 'mysheet' then specify it as 'mysheet$"

Since I have no idea what you typed in I cannot be of much the code works on any and all worksheets in all versions of Excel.

February 8th, 2015 8:24pm

Note that the sheet name has to end with a dollar sign.  If sheet is named 'mysheet' then specify it as 'mysheet$"

Since I have no idea what you typed in I cannot be of much the code works on any and all worksheets in all versions of Excel.

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 11:05pm

Does you spreadsheet have column headers?

Why do you remove all of the formatting from the code?  Proper spacing an indenting makes the code readable. You code is difficult to read.

February 8th, 2015 11:18pm

yes it has


Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 11:20pm

There is something odd about your spreadsheet.  THismethod has been in use for more that a decade but can throw odd errors when a speradsheet is damaged or badly constructed.

Is it possible to up load a copy of the spreadsheet?

February 8th, 2015 11:29pm

There is something odd about your spreadsheet.  THismethod has been in use for more that a decade but can throw odd errors when a speradsheet is damaged or badly constructed.

Is it possible to up load a copy of the spread

Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 11:31pm

Upload here: http://pastebin.com/

February 9th, 2015 12:17am

This one allows zip files and no registration.

http://www.files.com/

Free Windows Admin Tool Kit Click here and download it now
February 9th, 2015 12:29am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics